package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.widget.Toast;

import com.example.myapplication.Fragment.BlankFragment;
import com.example.myapplication.Fragment.MineFragment;
import com.example.myapplication.Fragment.VideoFragment;
import com.example.myapplication.R;
import com.example.myapplication.tools.BarTransparent;
import com.example.myapplication.ui.dashboard.DashboardFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class BottomActivity extends AppCompatActivity {
    private long exitTime = 0;
    private ArrayList<Fragment> mFragmentList = new ArrayList<Fragment>();
    private Fragment mCurrentFragmen = null;  // 记录当前显示的Fragment
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarTransparent.makeStatusBarTransparent(BottomActivity.this);//状态栏透明
        BarTransparent.setAndroidNativeLightStatusBar(BottomActivity.this,true);//状态栏字体颜色变深色
        setContentView(R.layout.activity_bottom);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
//        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
//                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
//                .build();
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//        //NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//        NavigationUI.setupWithNavController(navView, navController);

        mFragmentList.add(0,new BlankFragment());
        mFragmentList.add(1,new DashboardFragment());
        mFragmentList.add(2,new VideoFragment());
        mFragmentList.add(3,new MineFragment());

       getSupportFragmentManager().beginTransaction()
                .add(R.id.nav_host_fragment, mFragmentList.get(0), null)
                .add(R.id.nav_host_fragment, mFragmentList.get(1), null)
                .add(R.id.nav_host_fragment, mFragmentList.get(2), null)
                .add(R.id.nav_host_fragment, mFragmentList.get(3), null)
                .hide(mFragmentList.get(1))
               .hide(mFragmentList.get(2))
               .hide(mFragmentList.get(3))
                .commit();
        mCurrentFragmen = mFragmentList.get(0);

        navView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.home:
                    getSupportFragmentManager().beginTransaction()
                            .hide(mCurrentFragmen)
                            .show(mFragmentList.get(0))
                            .commitNow();
                    mCurrentFragmen = mFragmentList.get(0);
                    break;
                case R.id.find:
                    getSupportFragmentManager().beginTransaction()
                            .hide(mCurrentFragmen)
                            .show(mFragmentList.get(1))
                            .commitNow();
                    mCurrentFragmen = mFragmentList.get(1);
                    break;
                case R.id.video:
                    getSupportFragmentManager().beginTransaction()
                            .hide(mCurrentFragmen)
                            .show(mFragmentList.get(2))
                            .commitNow();
                    mCurrentFragmen = mFragmentList.get(2);
                    break;
                case R.id.mine:
                    getSupportFragmentManager().beginTransaction()
                            .hide(mCurrentFragmen)
                            .show(mFragmentList.get(3))
                            .commitNow();
                    mCurrentFragmen = mFragmentList.get(3);
                    break;
            }
            return true;
        });
    }


    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - exitTime>2000){
            Toast.makeText(getApplicationContext(), "再按一次退出", Toast.LENGTH_SHORT).show();
            exitTime=System.currentTimeMillis();
        }
        else{
            super.onBackPressed();
            System.exit(0);
        }
    }
}